﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using Aop.Api;
using Aop.Api.Domain;
using Aop.Api.Request;
using WAD.Client2.Ali;
using WAD.DAL;
using WAD.Client2.WXPay;

namespace WAD.WEB.Client2.Ali
{
    public partial class Index : System.Web.UI.Page
    {
         
        protected void Page_Load(object sender, EventArgs e)
        {

            //var user = Pigeon.Framework.BLL.Users.User.GetCookie();
            //=============================获得订单信息================================
            string order_no = this.Request.QueryString["orderno"]??"";


            //检查参数是否正确
            if (string.IsNullOrEmpty(order_no))
            {
                Response.Write("对不起，您提交的参数有误！");
                return;
            }
            var order = DAO.Get("fc_order", $"orderid='{order_no}'", "");
            if (order == null) {
                Response.Write("对不起，您提交的参数有误！");
                return;
            }
            var client=new DefaultAopClient(AlipayConfig.URL, AlipayConfig.APPID, 
                AlipayConfig.RSA_PRIVATE_KEY, AlipayConfig.FORMAT, AlipayConfig.CHARSET,
                AlipayConfig.SIGNTYPE,AlipayConfig.ALIPAY_PUBLIC_KEY);
            var alipayRequest=new AlipayTradeWapPayRequest();
            var model = new AlipayTradeWapPayModel
            {
                OutTradeNo = order_no,
                Subject = "订阅宝",
                TotalAmount = order["pay_price"].ToString(),
                TimeoutExpress = "90m",
                ProductCode = "QUICK_WAP_PAY"
            };
            alipayRequest.SetBizModel(model);
            alipayRequest.SetNotifyUrl(AlipayConfig.notify_url);
            alipayRequest.SetReturnUrl(AlipayConfig.return_url);
            try
            {
                var form = client.pageExecute(alipayRequest).Body;
                Response.ContentType = "text/html;charset=" + AlipayConfig.CHARSET;
                
                Response.Write(form);
                Response.End();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                Response.Write(ex.StackTrace);
                Response.End();
            }
           
        }
    }
}